﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Base.Data;
using Base.Extensions;
using UnityEngine;
using UnityEngine.UI;
using Base.Custom;
using System.Linq;
namespace endit
{
    public enum 生成json类型
    {
        属性数据,
        系统数据,
        关系数据,
        材质数据
    }

    public class CreateJson : MonoBehaviour
    {
        public 生成json类型 jsonType;
        public Transform parent;
        public string jsonName;
        public string dboName;
        private bool isstart = false;
        private DBOperation dbo;
        private Dictionaries.MessageJson msgjson;
        private Transform[] trans;
        private Slider slider;
        private SystemMsg systenmsg;
        private List<string> dbnamelist;
        // Use this for initialization
        void Start()
        {
            dbo = new DBOperation();
            trans = parent.GetComponentsInChildren<Transform>();
            switch (jsonType)
            {
                case 生成json类型.属性数据:
                    msgjson = new Dictionaries.MessageJson();
                    msgjson.message = new List<Message>();
                    break;
                case 生成json类型.系统数据:
                    systenmsg = new SystemMsg();
                    SetSystemJson();
                    break;
                case 生成json类型.关系数据:
                    SetRelationJson();
                    break;
                case 生成json类型.材质数据:

                    break;
                default:
                    break;
            }
        }
        private int sum;
        // Update is called once per frame
        void Update()
        {

            if (isstart)
            {
                属性数据化json();
                isstart = false;
            }


        }
        public void 数据json化(Slider _slider)
        {
            slider = _slider;
            isstart = true;
        }
        void 属性数据化json()
        {
            if (isstart)
            {
                if (sum < trans.Length)
                {
                    if (trans[sum].GetComponent<MeshFilter>())
                    {
                        string id = trans[sum].name.GetID();
                        Message messager = new Message();
                        messager.key = id;
                        messager.property = dbo.getElementByID(id, dboName);
                        if (messager.property.Length != 0)
                        {
                            msgjson.message.Add(messager);
                        }
                        slider.value = (float)((float)sum / (float)trans.Length);
                        print(slider.value * 100 + "%");
                    }
                    sum++;
                }
                else
                {
                    string json = JsonUtility.ToJson(msgjson);
                    string savePath = Application.dataPath + "/Resources/Config/" + jsonName + ".json";
                    File.WriteAllText(savePath, json, Encoding.UTF8);
                    isstart = false;
                    slider.value = (float)((float)sum / (float)trans.Length);
                    print(slider.value * 100 + "%");
                }

            }
        }
        void SetSystemJson()
        {
            systenmsg.systemtype = SetSystemFloor();
            string json = JsonUtility.ToJson(systenmsg);
            string savePath = Application.dataPath + "/Resources/Config/SystemMsg.json";
            File.WriteAllText(savePath, json, Encoding.UTF8);
        }
        List<Floor> SetSystemFloor()
        {
            List<SqlAnd> sqland = new List<SqlAnd>();
            Floor floor = new Floor();
            List<Floor> _floor = new List<Floor>();
            //3楼
            floor.name = "3楼";
            sqland.Add(new SqlAnd("dbname", "JDAL3"));
            sqland.Add(new SqlAnd("MEP", "水系统"));
            floor.open.Add("JDAL3");
            floor.off.Add("JDAL3");
            floor.emp.Add(SetSystemEMP(sqland, "水系统"));
            if (sqland.Count > 2)
            {
                sqland.RemoveRange(2, sqland.Count - 2);
            }
            sqland[1] = new SqlAnd("MEP", "风系统");
            floor.emp.Add(SetSystemEMP(sqland, "风系统"));
            //sqland[1] = new SqlAnd("MEP", "电系统");
            //floor.emp.Add(SetSystemEMP(sqland, "电系统"));
            _floor.Add(floor);
            //2楼
            sqland = new List<SqlAnd>() { new SqlAnd("", ""), new SqlAnd("", "") };
            floor = new Floor();
            floor.name = "2楼";
            floor.open.Add("AWAL2");
            floor.off.Add("AWAL2");
            string str = "";
            sqland[0] = new SqlAnd("dbname", "JDAL2");
            sqland[1] = new SqlAnd("MEP", "水系统");
            floor.emp.Add(SetSystemEMP(sqland, "水系统"));
            if (sqland.Count > 2)
            {
                sqland.RemoveRange(2, sqland.Count - 2);
            }
            sqland[1] = new SqlAnd("MEP", "风系统");
            floor.emp.Add(SetSystemEMP(sqland, "风系统"));
            #region 组合
            //for (int i = 0; i < 3; i++)
            //{
            //    switch (i)
            //    {
            //        case 0:
            //            str = "JDAL2V1";
            //            break;
            //        case 1:
            //            str = "JDAL2V2";
            //            break;
            //        case 2:
            //            str = "JDAL2V3";
            //            break;
            //    }
            //    if (sqland.Count > 2)
            //    {
            //        sqland.RemoveRange(2, sqland.Count - 2);
            //    }
            //    if (floor.emp.Count==0)
            //    {
            //        sqland[0] = new SqlAnd("dbname", str);
            //        sqland[1] = new SqlAnd("MEP", "水系统");
            //        floor.emp.Add(SetSystemEMP(sqland, "水系统"));
            //        if (sqland.Count > 2)
            //        {
            //            sqland.RemoveRange(2, sqland.Count - 2);
            //        }
            //        sqland[1] = new SqlAnd("MEP", "风系统");
            //        floor.emp.Add(SetSystemEMP(sqland, "风系统"));
            //        //sqland[1] = new SqlAnd("MEP", "电系统");
            //        //floor.emp.Add(SetSystemEMP(sqland, "电系统"));
            //    }
            //    else
            //    {
            //        sqland[0] = new SqlAnd("dbname", str);
            //        sqland[1] = new SqlAnd("MEP", "水系统");
            //        SetSystemEMP(sqland, "水系统", floor.emp.Find(s => s.name == "水系统"));
            //        if (sqland.Count > 2)
            //        {
            //            sqland.RemoveRange(2, sqland.Count - 2);
            //        }
            //        sqland[1] = new SqlAnd("MEP", "风系统");
            //        SetSystemEMP(sqland, "水系统", floor.emp.Find(s => s.name == "风系统"));
            //        //sqland[1] = new SqlAnd("MEP", "电系统");
            //        //floor.emp.Add(SetSystemEMP(sqland, "电系统"));
            //    }




            //}
            #endregion
            _floor.Add(floor);
            //1楼
            sqland = new List<SqlAnd>() { new SqlAnd("", ""), new SqlAnd("", "") };
            floor = new Floor();
            floor.name = "1楼";
            floor.open.Add("AWAL1");
            floor.off.Add("AWAL1");

            sqland[0] = new SqlAnd("dbname", "JDAL1");
            sqland[1] = new SqlAnd("MEP", "水系统");
            floor.emp.Add(SetSystemEMP(sqland, "水系统"));
            if (sqland.Count > 2)
            {
                sqland.RemoveRange(2, sqland.Count - 2);
            }
            sqland[1] = new SqlAnd("MEP", "风系统");
            floor.emp.Add(SetSystemEMP(sqland, "风系统"));
            //sqland[1] = new SqlAnd("MEP", "电系统");
            //floor.emp.Add(SetSystemEMP(sqland, "电系统"));
            _floor.Add(floor);
            //B1楼
            sqland = new List<SqlAnd>() { new SqlAnd("", ""), new SqlAnd("", "") };
            floor = new Floor();
            floor.name = "B1楼";
            floor.off.Add("AWAB1");
            floor.open.Add("AWAB1");
            sqland[0] = new SqlAnd("dbname", "JDAB1");
            sqland[1] = new SqlAnd("MEP", "水系统");
            floor.emp.Add(SetSystemEMP(sqland, "水系统"));
            if (sqland.Count > 2)
            {
                sqland.RemoveRange(2, sqland.Count - 2);
            }
            sqland[1] = new SqlAnd("MEP", "风系统");
            floor.emp.Add(SetSystemEMP(sqland, "风系统"));
            //sqland[1] = new SqlAnd("MEP", "电系统");
            //floor.emp.Add(SetSystemEMP(sqland, "电系统"));
            _floor.Add(floor);
            return _floor;
        }
        EMP SetSystemEMP(List<SqlAnd> sqland, string name)
        {
            EMP emp = new EMP();
            emp.name = name;
            emp.id = dbo.getAttributeList(sqland);//查询ID楼层下大系统的所有id返回list<string>
            List<string> type = dbo.getAttributeList(sqland, "系统类型", true);//查询ID楼层下大系统的所有系统分类返回list<string>
            if (sqland.Count > 2)
            {
                sqland.RemoveRange(2, sqland.Count - 2);
            }
            if (sqland.Count < 3)
            {
                sqland.Add(new SqlAnd("系统类型", ""));
            }
            foreach (var item in type)
            {
                sqland[2].value = item;
                emp.type.Add(SetSystemType(sqland, item));
            }
            return emp;
        }
        EMP SetSystemEMP(List<SqlAnd> sqland, string name, EMP emp)
        {
            emp.id.AddRange(dbo.getAttributeList(sqland));
            //emp.id = dbo.getAttributeList(sqland);//查询ID楼层下大系统的所有id返回list<string>
            List<string> type = dbo.getAttributeList(sqland, "系统类型", true);//查询ID楼层下大系统的所有系统分类返回list<string>
            if (sqland.Count > 2)
            {
                sqland.RemoveRange(2, sqland.Count - 2);
            }
            if (sqland.Count < 3)
            {
                sqland.Add(new SqlAnd("系统类型", ""));
            }
            foreach (var item in type)
            {
                sqland[2].value = item;
                emp.type.Add(SetSystemType(sqland, item));
            }
            return emp;
        }
        SystemType SetSystemType(List<SqlAnd> sqland, string name)
        {
            SystemType type = new SystemType();
            type.name = name;
            if (sqland.Count > 3)
            {
                sqland.RemoveRange(3, sqland.Count - 3);
            }
            type.id = dbo.getAttributeList(sqland);//查询该系统分类下的所有ID返回list<string>
            List<string> ststemName = dbo.getAttributeList(sqland, "系统名称", true);//查询该系统分类下所有系统返回list<string> 系统名称列表
            if (sqland.Count < 4)
            {
                sqland.Add(new SqlAnd("系统名称", ""));
            }
            foreach (var item in ststemName)
            {
                sqland[3].value = item;
                type.system.Add(SetSystemName(sqland, item));
            }
            return type;
        }
        SystemName SetSystemName(List<SqlAnd> sql, string name)
        {
            SystemName systemName = new SystemName();
            systemName.name = name;
            systemName.id = dbo.getAttributeList(sql);//查询系统名称下所有ID
                                                      //查询传入的系统下的所有对象id
            return systemName;
        }

        [Serializable]
        class Maintain
        {
            public string name;
            public string maintain;
            public string texter;
            public string color;
            public Maintain() { }
            public Maintain(string _name,string _maintain, string _texter,string _color)
            {
                name = _name;
                maintain = _maintain;
                texter = _texter;
                color = _color;
            }
        }

        /// <summary>
        /// SystemPath.json生成 关系中的系统列表
        /// </summary>
        void SetRelationJson()
        {
            int sum = 1;
            string[] meplist = new string[] { "水系统", "风系统", "电系统" };
            string[] dboname = new string[] { "JDAB1", "JDAL1", "JDAL3" };
            Dictionaries.SystemJsonone jsonone = new Dictionaries.SystemJsonone();
            jsonone.json = new List<SystemJsonList>();
            SystemJsonList jsonlis = new SystemJsonList();
            for (int i = 0; i < meplist.Length; i++)
            {
                List<string> type = new List<string>();
                foreach (var db in dboname)
                {
                    List<SqlAnd> sqland = new List<SqlAnd>() { new SqlAnd("dbname", db), new SqlAnd("MEP", meplist[i]) };
                    type.AddRange(dbo.getAttributeList(sqland, "系统名称", true));
                    type = type.Distinct().ToList();
                }



                foreach (var item in type)
                {
                    SystemJson json = new SystemJson();
                    json.code = sum.ToString();
                    json.name = item;
                    foreach (var _db in dboname)
                    {
                        json.objid.AddRange(dbo.getAttributeList(new List<SqlAnd>() { new SqlAnd("dbname", _db), new SqlAnd("MEP", meplist[i]), new SqlAnd("系统名称", item) }));
                        json.objid = json.objid.Distinct().ToList();
                    }
                    sum++;
                    switch (i)
                    {
                        case 0:
                            jsonlis.shui.Add(json);
                            break;
                        case 1:
                            jsonlis.feng.Add(json);
                            break;
                        case 2:
                            jsonlis.dian.Add(json);
                            break;
                    }
                }
            }
            jsonone.json.Add(jsonlis);
            string jsonx = JsonUtility.ToJson(jsonone);
            string savePath = Application.dataPath + "/Resources/Config/SystemPath.json";
            File.WriteAllText(savePath, jsonx, Encoding.UTF8);

        }


        void 生成匹配材质()
        {
            List<string> name = new List<string>();
            List<string> id = new List<string>();
            List<Maintain> maintains = new List<Maintain>();
            foreach (var item in trans)
            {
                MeshFilter mesh = item.GetComponent<MeshFilter>();
                if (mesh!=null)
                {
                    name.Add(item.name.Split('[')[0]);
                    
                }
            }
            name = name.Distinct().ToList();
            foreach (var item in trans)
            {
                MeshFilter mesh = item.GetComponent<MeshFilter>();
                if (mesh != null)
                {
                    if (!id.Contains(item.name.Split('[')[0]))
                    {
                        string textername = "";
                        string maintain = "根据id查找数据库";
                        //maintains.Add(new Maintain(item.name.Split('[')[0],));
                    }
                }
            }

        }



    }
}